package cn.controller;

import cn.dto.Query;
import cn.entity.*;
import cn.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("admin")
public class AdminController{
    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "login",method = RequestMethod.GET)
    public String loginPage(){
        return "admin/login";
    }

    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String login(String username, String password, Model model, HttpSession session){
        System.out.println(username+password);
        if(username != null && password !=null){
            int i = adminService.login(username, password);
            if(i > 0){
                model.addAttribute("username",username);
                session.setAttribute("username",username);
//                return "admin/login";
                return "admin/index";
            }
        }
//        return "admin/error";
        return "admin/pages/public/failure";
    }
    //todo  分页
    @RequestMapping(value = "/pages/home/banner",method = RequestMethod.GET)
    public String getBannerList(Model model, Query query){
        List<Banner> bannerList=adminService.selectBannerPage(query);
        if(bannerList != null && bannerList.size() > 0){
            int total = adminService.totalBannerNum();
            model.addAttribute("bannerList",bannerList);
            model.addAttribute("total",total);
            model.addAttribute("current",query.getPage());
            model.addAttribute("pages",query.getPages(total));
        }
        return "admin/pages/home/banner";
    }

    @RequestMapping(value = "/banner/add",method = RequestMethod.POST)
    public String addBanner(MultipartFile banner,HttpServletRequest request) throws IOException {
        String savePath = request.getServletContext().getRealPath("/");
        if(adminService.saveBanner(savePath,banner) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }

    @RequestMapping(value = "/banner/edit",method = RequestMethod.POST)
    public String updateBanner(MultipartFile banner,Integer id,HttpServletRequest request) throws IOException {
        String savePath = request.getServletContext().getRealPath("/");
        if(adminService.updateBanner(savePath,banner,id) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }
    @RequestMapping(value = "/banner/delete",method = RequestMethod.POST)
    public String deleteBanner(Integer id,HttpServletRequest request) {
        String savePath = request.getServletContext().getRealPath("/");
        if(adminService.deleteBanner(savePath,id) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }


    @RequestMapping(value = "/pages/home/news",method = RequestMethod.GET)
    public String getNewsList(Model model, Query query){
        List<News> newsList=adminService.selectNewsPage(query);
        if(newsList != null && newsList.size() > 0){
            int total = adminService.totalNewsNum();
            model.addAttribute("newsList",newsList);
            model.addAttribute("total",total);
            model.addAttribute("current",query.getPage());
            model.addAttribute("pages",query.getPages(total));
        }
        return "admin/pages/home/news";
    }

    @RequestMapping(value = "/news/add",method = RequestMethod.POST)
    public String addNews(News news){
        if(adminService.insertNews(news) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }

    @RequestMapping(value = "/news/news-edit",method = RequestMethod.GET)
    public String selectNewsById(Model model,Integer id){
        News news = adminService.selectNewsById(id);
        if(news != null){
            model.addAttribute("news",news);
        }
        return "admin/pages/home/news-edit";
    }
    @RequestMapping(value = "/news/edit",method = RequestMethod.POST)
    public String updateNews(News news){
        if(adminService.updateNews(news) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }
    @RequestMapping(value = "/news/delete",method = RequestMethod.POST)
    public String deleteNews(Integer id) {
        if(adminService.deleteNews(id) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }

    //基本没问题
    @RequestMapping(value = "/pages/home/group-desc",method = RequestMethod.GET)
    public String getIntroduction(Model model){
        Introduction introduction=adminService.selectLastIntroduction();
        if(introduction != null){
            model.addAttribute("introduction",introduction);
        }
        return "admin/pages/home/group-desc";
    }

    @RequestMapping(value = "/introduction/image/edit",method = RequestMethod.POST)
    public String updateIntroductionImage(MultipartFile img,Integer id,HttpServletRequest request) throws IOException {
        String savePath = request.getServletContext().getRealPath("/");
        if(adminService.updateIntroductionImage(savePath,img,id) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }
    @RequestMapping(value = "/introduction/content/edit",method = RequestMethod.POST)
    public String updateIntroductionContent(Introduction introduction) {
        if(adminService.updateIntroductionContent(introduction) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }

    //todo 没有科研项目和劳动成果
    @RequestMapping(value = "/pages/members/member",method = RequestMethod.GET)
    public String getMemberList(Model model,Query query){
        query.setOffset();
        List<Member> memberList = adminService.selectMemberPage(query);
        if(memberList != null && memberList.size()>0){
            int total = adminService.totalMemberNum();
            model.addAttribute("memberList",memberList);
            model.addAttribute("total",total);
            model.addAttribute("current",query.getPage());
            model.addAttribute("pages",query.getPages(total));
        }
        return "/admin/pages/members/member";
    }
    @RequestMapping(value = "/member/member-edit",method = RequestMethod.GET)
    public String selectMemberById(Model model,Integer id) {
        Member member = adminService.selectMemberById(id);
        if(member != null){
            List<MemberMsg> memberMsgList = adminService.selectMemberMsgByMemId(id);
            List<SubMenu> subMenuList = adminService.selectSubMenuByMenu("member");
            for(MemberMsg memberMsg:memberMsgList){
                switch(memberMsg.getType()){
                    case 0://经历
                        model.addAttribute("experience",memberMsg.getMsg());
                        break;
                    case 1://荣誉称号
                        model.addAttribute("honoraryTitle",memberMsg.getMsg());
                        break;
                    case 2://科研项目
                        model.addAttribute("project",memberMsg.getMsg());
                        break;
                    case 3://成果
                        model.addAttribute("achievement",memberMsg.getMsg());
                        break;
                }
            }
            System.out.println(memberMsgList);
            model.addAttribute("member",member);
            model.addAttribute("subMenuList",subMenuList);
        }
        return "admin/pages/members/member-edit";
    }
    @RequestMapping(value = "/member/edit",method = RequestMethod.POST)
    public String updateMember(Member member,MultipartFile img,HttpServletRequest request,
                               String experience,String honoraryTitle,String project,String achievement) throws IOException {
        String savePath = request.getServletContext().getRealPath("/");
        List<MemberMsg> memberMsgList = trans2Msg(member.getId(),experience,honoraryTitle,project,achievement);
        if(adminService.updateMember(member,memberMsgList,savePath,img) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }
    @RequestMapping(value = "/member/member-add",method = RequestMethod.GET)
    public String toAddMember(Model model){
        List<SubMenu> subMenuList = adminService.selectSubMenuByMenu("member");
        model.addAttribute("subMenuList",subMenuList);
        return "admin/pages/members/member-add";
    }
    @RequestMapping(value = "/member/add",method = RequestMethod.POST)
    public String insertMember(MultipartFile img, Member member,HttpServletRequest request,
                               String experience,String honoraryTitle,String project,String achievement) throws IOException {
        // 保存文件 memberMSG
        List<MemberMsg> memberMsgList = trans2Msg(member.getId(),experience,honoraryTitle,project,achievement);
        String savePath = request.getServletContext().getRealPath("/");
        if(adminService.insertMember(member,savePath,img,memberMsgList) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }

    private List<MemberMsg> trans2Msg(Integer memberId,String experience, String honoraryTitle, String project, String achievement) {
        List<MemberMsg> memberMsgList = new ArrayList<>();
        if(experience != null && experience.length() > 0){
            memberMsgList.add(new MemberMsg(null,experience,(byte)0,memberId));
        }
        if(honoraryTitle != null && honoraryTitle.length() > 0){
            memberMsgList.add(new MemberMsg(null,honoraryTitle,(byte)1,memberId));
        }
        if(project != null && project.length() > 0){
            memberMsgList.add(new MemberMsg(null,project,(byte)2,memberId));
        }
        if(achievement != null && achievement.length() > 0){
            memberMsgList.add(new MemberMsg(null,achievement,(byte)3,memberId));
        }
        return memberMsgList;
    }

    @RequestMapping(value = "/member/delete",method = RequestMethod.POST)
    public String deleteMember(Integer id,HttpServletRequest request){
        String savePath = request.getServletContext().getRealPath("/");
        if(adminService.deleteMember(id,savePath) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }
    @RequestMapping(value = "/pages/papers/paper",method = RequestMethod.GET)
    public String selectPaperPage(Model model,Query query){
        List<Paper> paperList = adminService.selectPaperPage(query);
        if(paperList != null && paperList.size()>0){
            int total = adminService.totalPaperNum();
            model.addAttribute("paperList",paperList);
            model.addAttribute("total",total);
            model.addAttribute("current",query.getPage());
            model.addAttribute("pages",query.getPages(total));
        }
        return "admin/pages/papers/paper";
    }
    @RequestMapping(value = "/paper/paper-edit",method = RequestMethod.GET)
    public String updatePaper(Model model,Integer id){
        Paper paper = adminService.selectPaperById(id);
        if(paper != null){
            model.addAttribute("paper",paper);
        }
        return "admin/pages/papers/paper-edit";
    }
    @RequestMapping(value = "/paper/edit",method = RequestMethod.POST)
    public String updatePaper(Paper paper){
        if(adminService.updatePaper(paper) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }
    @RequestMapping(value = "/paper/add",method = RequestMethod.POST)
    public String insertPaper(Paper paper){
        if(adminService.insertPaper(paper) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }
    @RequestMapping(value = "/paper/delete",method = RequestMethod.POST)
    public String insertPaper(Integer id){
        if(adminService.deletePaper(id) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }


    @RequestMapping(value = "/pages/lab/machine",method = RequestMethod.GET)
    public String getMachineList(Model model,Query query){
        query.setOffset();
        List<Machine> machineList = adminService.selectMachinePage(query);
        if(machineList != null && machineList.size()>0){
            int total = adminService.totalMachineNum();
            model.addAttribute("machineList",machineList);
            model.addAttribute("total",total);
            model.addAttribute("current",query.getPage());
            model.addAttribute("pages",query.getPages(total));
        }
        return "/admin/pages/lab/machine";
    }
    @RequestMapping(value = "/machine/machine-edit",method = RequestMethod.GET)
    public String selectMachineById(Model model,Integer id) {
        Machine machine = adminService.selectMachineById(id);
        if(machine != null){
            model.addAttribute("machine",machine);
        }
        return "admin/pages/lab/machine-edit";
    }
    @RequestMapping(value = "/machine/edit",method = RequestMethod.POST)
    public String updateMachine(MultipartFile img,Machine machine,HttpServletRequest request) throws IOException {
        String savePath = request.getServletContext().getRealPath("/");
        if(adminService.updateMachine(machine,savePath,img) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }
    @RequestMapping(value = "/machine/add",method = RequestMethod.POST)
    public String insertMachine(MultipartFile img, Machine machine,HttpServletRequest request) throws IOException {
        String savePath = request.getServletContext().getRealPath("/");
        if(adminService.insertMachine(machine,img,savePath) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }
    @RequestMapping(value = "/machine/delete",method = RequestMethod.POST)
    public String deleteMachine(Integer id,HttpServletRequest request){
        String savePath = request.getServletContext().getRealPath("/");
        if(adminService.deleteMachine(id,savePath) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }


    @RequestMapping(value = "/pages/research/research",method = RequestMethod.GET)
    public String getResearchList(Model model, Query query){
        List<Research> researchList=adminService.selectResearchPage(query);
        if(researchList != null && researchList.size() > 0){
            List<SubMenu> subMenuList = adminService.selectSubMenuByMenu("research");
            int total = adminService.totalResearchNum();
            model.addAttribute("researchList",researchList);
            model.addAttribute("subMenuList",subMenuList);
            model.addAttribute("total",total);
            model.addAttribute("current",query.getPage());
            model.addAttribute("pages",query.getPages(total));
        }
        return "admin/pages/research/research";
    }

    @RequestMapping(value = "/research/add",method = RequestMethod.POST)
    public String addResearch(Research research){
        if(adminService.insertResearch(research) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }

    @RequestMapping(value = "/research/research-edit",method = RequestMethod.GET)
    public String selectResearchById(Model model,Integer id){
        Research research = adminService.selectResearchById(id);
        if(research != null){
            model.addAttribute("research",research);
        }
        return "admin/pages/research/research-edit";
    }

    @RequestMapping(value = "/research/edit",method = RequestMethod.POST)
    public String updateResearch(Research research){
        if(adminService.updateResearch(research) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }
    @RequestMapping(value = "/research/delete",method = RequestMethod.POST)
    public String deleteResearch(Integer id) {
        if(adminService.deleteResearch(id) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }
    @RequestMapping(value = "/pages/contact/contact",method = RequestMethod.GET)
    public String getContact(Model model,@RequestParam(value = "menuId",defaultValue = "7") Integer menuId){
        Contact contact=adminService.selectContactByMenuId(menuId);
        if(contact != null){
            model.addAttribute("contact",contact);
        }
        return "admin/pages/contact/contact";
    }
    @RequestMapping(value = "/contact",method = RequestMethod.POST)
    public String updateContact(Contact contact){
        if(adminService.updateContact(contact) > 0){
            return "admin/pages/public/success";
        }
        return "admin/pages/public/failure";
    }
}
